package com.feonix.complat.listener;

import java.util.Collection;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.springframework.web.context.ContextLoaderListener;

import com.feonix.common.BaseInfo;
import com.feonix.common.basedao.EntityManager;
import com.feonix.common.datasource.DataSourceSwitch;
import com.feonix.common.delegate.OnSystemStartDelegate;
import com.feonix.common.task.BaseTask;
import com.feonix.common.task.LicenceCheckTask;
import com.feonix.common.task.TaskManager;
import com.feonix.common.util.FreemarkerUtil;
import com.feonix.common.util.SpringUtil;
import com.feonix.common.view.component.ComponentConfiger;

/**
 * Application Lifecycle Listener implementation class BaseListener
 *
 */
public class BaseListener extends ContextLoaderListener implements ServletContextListener, ServletRequestListener {

	protected final Log logger = LogFactory.getLog(BaseListener.class);

	/**
	 * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
	 */
	public void requestDestroyed(ServletRequestEvent arg0) {
		DataSourceSwitch.changeDefault();
	}

	/**
	 * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
	 */
	public void requestInitialized(ServletRequestEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		Collection<OnSystemStartDelegate> systemStartDelegateCollection = SpringUtil
				.getBeans(OnSystemStartDelegate.class);
		if (systemStartDelegateCollection != null) {
			for (OnSystemStartDelegate systemStartDelegate : systemStartDelegateCollection) {
				systemStartDelegate.onDestroyed();
			}
		}
		TaskManager.stopAllTask();
		ProxoolFacade.shutdown();
		super.contextDestroyed(servletContextEvent);
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent paramServletContextEvent) {
		try {
			FreemarkerUtil.initFreemarker(paramServletContextEvent.getServletContext());
			BaseInfo.init(paramServletContextEvent.getServletContext());
			super.contextInitialized(paramServletContextEvent);
			EntityManager.init();
			ComponentConfiger.initComponent();
			// SecurityUtil.init(BaseInfo.getRealPath());
			TaskManager.addTask(new BaseTask[] { new LicenceCheckTask() });
			if (BaseInfo.isPrepared()) {
				EntityManager.checkTableAndCreate();
				Collection<OnSystemStartDelegate> collection = SpringUtil.getBeans(OnSystemStartDelegate.class);
				if (collection != null)
					for (OnSystemStartDelegate localOnSystemStartDelegate : collection) {
						localOnSystemStartDelegate.onStart();
					}

			}
		} catch (Exception localException) {
			this.logger.error("contextInitialized error", localException);
		}
	}

}
